package com.ayoka.parenting.interf;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Created by jinchunping on 2019/1/31.
 */

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        // 当不滑动时
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            //获取最后一个完全显示的itemPosition
            int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
            int itemCount = manager.getItemCount();
            int firstItemPosition = manager.findFirstCompletelyVisibleItemPosition();

            // 判断是否滑动到了第一一个item，并且是向上滑动
            if (firstItemPosition == 0 && ((lastItemPosition - firstItemPosition + 1) < itemCount)) {
                //加载更多
                onTopLoadMore(lastItemPosition);
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
    }

    /**
     * 加载更多回调
     *
     * @param lastItemPosition
     */
    public abstract void onTopLoadMore(int lastItemPosition);
}
